from langchain.output_parsers import DatetimeOutputParser
from openaiConfigurations import openai_api_key, openai_api_base
from langchain.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI

prompt = ChatPromptTemplate.from_template(
    "what time was {event} (in %Y-%m-%dT%H:%M:%S.%fz fromat - only return this value)"
)

openai_35 = ChatOpenAI(api_key = openai_api_key, base_url = openai_api_base) | DatetimeOutputParser()
openai_4 = ChatOpenAI(model = "gpt-4", 
                      api_key = openai_api_key, 
                      base_url = openai_api_base
           ) | DatetimeOutputParser()

only_35 = prompt | openai_35
fallback_4 = prompt | openai_35.with_fallbacks([openai_4])

# 先使用GPT-3.5解析
try:
    print(only_35.invoke({"event": "the superbowl in 1994"}))
except Exception as e:
    print(f"Error: {e}")

# 注释：实际上这里的gpt-3.5已经可以处理该任务，不需要使用更高级的模型，如果需要，尝试以下代码
try:
    print(fallback_4.invoke({"event": "the superbowl in 1994"}))
except Exception as e:
    print(f"Error: {e}")
